//
//  HeaderView.m
//  QQ好友列表
//
//  Created by Hubery on 15/6/27.
//  Copyright (c) 2015年 Whx. All rights reserved.
//

#import "HeaderView.h"
#import "AddressGroupModel.h"
@interface HeaderView()
@property(nonatomic,weak) UIButton *btn;
@property(nonatomic,weak) UILabel *label;
@end

@implementation HeaderView

+(instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"header";
    HeaderView *headerView =[tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
    if(headerView == nil)
    {
        headerView = [[HeaderView alloc]initWithReuseIdentifier:identifier];
    }
    return headerView;
}

-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithReuseIdentifier:reuseIdentifier])//重写系统的方法 要调用系统的该方法
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        //设置内容左对齐
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        //设置内边距
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 15);
        
       //[btn setTitle:@"sss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        //设置 btn 图片不拉伸
        btn.imageView.contentMode = UIViewContentModeCenter;
        btn.imageView.clipsToBounds = NO;//设置当 image 超出范围不要剪切
        [self addSubview:btn];
        self.btn = btn;
        
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentRight;
        label.textColor = [UIColor grayColor];
        //label.backgroundColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:12];
        [self addSubview:label];
        self.label = label;
    }
    return self;
}

-(void)btnOnClick:(UIButton *)btn
{
    self.addressGroupModel.open = !self.addressGroupModel.open;
    if([self.delegate respondsToSelector:@selector( headerViewDidClickHeaderView:)])//判断代理有没有实现这个方法
    {
        [self.delegate headerViewDidClickHeaderView:self];
    }
}

- (void)setAddressGroupModel:(AddressGroupModel *)addressGroupModel
{
    _addressGroupModel = addressGroupModel;
    //[self.btn setTitle:_addressGroupModel.name forState:UIControlStateNormal];
}

#pragma mark - 当一个控件被添加到其他视图上的时候会调用
-(void)didMoveToSuperview//已经添加到父视图
{
    self.label.text = self.name;
    //这里可以拿到最新的视图
    if (self.addressGroupModel.open)//只有是不打开的时候才不转
    {
        self.btn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.btn.frame = self.bounds;
    self.label.frame = CGRectMake(0, 0, 190, self.bounds.size.height);
}
@end
